package com.geen.tuoke.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.MediaStore;

import com.geen.tuoke.MyApplication;
import com.geen.tuoke.bean.CallInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 文庆
 * @description
 */
public class CallUtil {

    private volatile static CallUtil instance;
    private static Context context = MyApplication.instance;

    /**
     * 返回单例模式
     */
    public static CallUtil getInstance() {
        if (instance == null) {
            synchronized (CallUtil.class) {
                if (instance == null) {
                    instance = new CallUtil();
                }
            }
        }
        return instance;
    }


    /**
     * https://www.cnblogs.com/scode2/p/9919221.html
     * <p>
     * 利用系统CallLog获取通话历史记录
     *
     * @param num 要读取记录的数量
     * @return
     */
    public List<CallInfo> getCallHistoryList(int num) {

        List<CallInfo> callInfos = new ArrayList<>();

        final Bundle bundle = new Bundle();
        //  CallLog.Calls.TYPE,  //呼入/呼出(2)/未接  这里如果传值type=2  取出来的都是呼出的
        bundle.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, "type=2");

        bundle.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, null);
//        bundle.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION,
//                ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);

        bundle.putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER,
                "date DESC");
        bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, 1);
        bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, 0);

        Cursor cs = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            cs = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, //系统方式获取通讯录存储地址
                    new String[]{
                            CallLog.Calls.CACHED_NAME,  //姓名
                            CallLog.Calls.NUMBER,    //号码
                            CallLog.Calls.TYPE,  //呼入/呼出(2)/未接
                            CallLog.Calls.DATE,  //拨打时间
                            CallLog.Calls.DURATION,   //通话时长
                            CallLog.Calls.GEOCODED_LOCATION,   //地区
                    }, bundle,null);
        }else{
             cs = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, //系统方式获取通讯录存储地址
                    new String[]{
                            CallLog.Calls.CACHED_NAME,  //姓名
                            CallLog.Calls.NUMBER,    //号码
                            CallLog.Calls.TYPE,  //呼入/呼出(2)/未接
                            CallLog.Calls.DATE,  //拨打时间
                            CallLog.Calls.DURATION,   //通话时长
                            CallLog.Calls.GEOCODED_LOCATION,   //地区
                    }, "  type=2 ", null, "date DESC LIMIT " + num + " OFFSET 0");
        }
        int i = 0;
        if (cs != null && cs.getCount() > 0) {

            for (cs.moveToFirst(); (!cs.isAfterLast()) && i < num; cs.moveToNext(), i++) {
                String callName = cs.getString(0);  //名称
                String callNumber = cs.getString(1);  //号码
                //如果名字为空，在通讯录查询一次有没有对应联系人
                if (callName == null || callName.equals("")) {
                    String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME};
                    //设置查询条件
                    String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + "='" + callNumber + "'";
                    Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            cols, selection, null, null);
                    int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
                    if (cursor.getCount() > 0) {
                        cursor.moveToFirst();
                        callName = cursor.getString(nameFieldColumnIndex);
                    }
                    cursor.close();
                }

                //通话类型
                int callType = Integer.parseInt(cs.getString(2));
                String callTypeStr = "";
                switch (callType) {
                    case CallLog.Calls.INCOMING_TYPE:
                        callTypeStr = "呼入";
                        break;
                    case CallLog.Calls.OUTGOING_TYPE:
                        callTypeStr = "呼出";
                        break;
                    default:
                        callTypeStr = "未接通";
                        break;
                }

                //拨打时间
                long timeStamp = Long.parseLong(cs.getString(3));


                //通话时长
                int callDuration = Integer.parseInt(cs.getString(4));

                String areaName = cs.getString(5);

                CallInfo callInfo = new CallInfo(callNumber, callTypeStr, callDuration, timeStamp);
                callInfos.add(callInfo);
            }
        }
        cs.close();
        return callInfos;
    }


}
